//
//  ScrollViewManager.m
//  Product_A
//
//  Created by lanou on 16/5/11.
//  Copyright © 2016年 万长亮. All rights reserved.
//

#import "ScrollViewManager.h"
@interface ScrollViewManager()<UIScrollViewDelegate>
@property (nonatomic ,strong)UIScrollView *scrollView;

@property (nonatomic, strong)UIPageControl *page;

@property (nonatomic, strong)NSArray *picArray;

@property (nonatomic, assign)CGRect frame;
@property (nonatomic, assign)NSInteger number;
@end
@implementation ScrollViewManager

+ (void)viewRepeatScrollWithPicArray:(NSArray *)picArray view:(UIView *)view frame:(CGRect)frame{
    if (picArray == nil || picArray.count == 0) {
        return;
    }
    ScrollViewManager *manager = [[ScrollViewManager alloc]init];
    [manager viewRepeatScrollWithPicArray:picArray view:view frame:frame];
}
- (void)viewRepeatScrollWithPicArray:(NSArray *)picArray view:(UIView *)view frame:(CGRect)frame{
    self.picArray = picArray;
    self.frame = frame;
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
    self.number = 1;
    self.scrollView = [[UIScrollView alloc]initWithFrame:frame];
    _scrollView.delegate = self;
    
    
    _scrollView.contentSize = CGSizeMake(frame.size.width *(picArray.count + 2), frame.size.height);
    // 改变偏移量 一上来显示 实际上是第二张图
    _scrollView.contentOffset = CGPointMake(frame.size.width * 1, 0);
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
//    _scrollView.backgroundColor = [UIColor magentaColor];
    [view addSubview:_scrollView];
    
    // 第一张图的最左边 放最后一张图 显示的时候 偏移量是从第二张开始
    UIImageView *tempImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    [tempImageView sd_setImageWithURL:[NSURL URLWithString:[picArray lastObject]] completed:nil];
    [_scrollView addSubview:tempImageView];
    
    // 最后一张图的右边 放第一张图
    UIImageView *tempImageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(frame.size.width * (picArray.count + 1), 0, frame.size.width, frame.size.height)];
    [tempImageView2 sd_setImageWithURL:[NSURL URLWithString:picArray[0]] placeholderImage:nil completed:nil];
    [_scrollView addSubview:tempImageView2];
    
    for (int i = 0; i < picArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(frame.size.width * (i + 1), 0,frame.size.width, frame.size.height)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:picArray[i]] placeholderImage:nil completed:nil];
        [_scrollView addSubview:imageView];
        
    }
    
    self.page = [[UIPageControl alloc]initWithFrame:CGRectMake(frame.size.width / 2 - 50 +frame.origin.x, frame.size.height + frame.origin.y - 30 , 100, 30)];
    _page.numberOfPages = self.picArray.count;
    //    _page.currentPageIndicatorTintColor = [UIColor redColor];
    //    _page.pageIndicatorTintColor = [UIColor whiteColor];
//    _page.currentPage = 0;
    [view addSubview:_page];

}



- (void)timerAction:(NSTimer *)timer{
    _page.currentPage = _number;
    NSInteger index = _scrollView.contentOffset.x / self.frame.size.width;
    [_scrollView setContentOffset:CGPointMake(self.frame.size.width * (_number + 1), 0) animated:YES];
    _number++;
    if (index == 0)
    {
        _number = self.picArray.count;
        [_scrollView setContentOffset:CGPointMake(self.frame.size.width * self.picArray.count, 0) animated:NO];
        _page.currentPage = self.picArray.count - 1;
    }
    else if (index == self.picArray.count + 1)
    {
        _number = 1;
        [_scrollView setContentOffset:CGPointMake(self.frame.size.width * 1, 0) animated:NO];
        _page.currentPage = 0;
    }

}
// 开始滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    if (scrollView != self.scrollView) {
//        return;
//    }
    NSInteger index = scrollView.contentOffset.x / self.frame.size.width;
        if (index > self.picArray.count)
    {
        _page.currentPage = 0;
    }
}
#pragma mark --- 结束减速触发的方法 ---
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//    if (scrollView != self.scrollView) {
//        return;
//    }
    
    NSInteger index = scrollView.contentOffset.x / self.frame.size.width;
  
    _page.currentPage = index - 1;
    if (index == 0)
    {
        [scrollView setContentOffset:CGPointMake(self.frame.size.width * self.picArray.count, 0) animated:NO];
        _page.currentPage = self.picArray.count - 1;
    }
    else if (index == self.picArray.count + 1)
    {
        [scrollView setContentOffset:CGPointMake(self.frame.size.width * 1, 0) animated:NO];
        _page.currentPage = 0;
    }
    _number = _page.currentPage;

}
@end
